﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WenFrame.Login
{
    public partial class SetPassword : WenSkin.Forms.WenForm
    {
        private readonly Form form;
        private readonly WenSkin.SQL.SqlConnect SqlConnect = new WenSkin.SQL.SqlConnect();
        private WenSkin.WenConfig WenConfig = new WenSkin.WenConfig();
        public SetPassword(Form form)
        {
            InitializeComponent();
            this.form = form;
        }

        private void wenButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(wenLableTextBox1.Text) || string.IsNullOrWhiteSpace(wenLableTextBox2.Text))
            {
                this.MsgBoxError("输入内容不能为空");
                return;
            }


            string sql = $"select [Password] from w_user where name=N'{UserInfo.UserName}'";

            if (SqlConnect.SqlToOneObject(sql)?.ToString() != wenLableTextBox1.Text)
            {
                this.MsgBoxError("原密码输入不正确");
                return;
            }

            string sqlSet = sqlsetpassword.TextFormat(UserInfo.UserName, wenLableTextBox2.Text);
            SqlConnect.SqlBool(sqlSet);
           
            wenLableTextBox1.Text = "";
            wenLableTextBox2.Text = "";

            this.MsgBoxWarning("重置密码成功!");
        }

        private void wenButton2_Click(object sender, EventArgs e)
        {
            form.Hide();
            this.Close();
            Task.Run(() =>
            {
                WenConfig["UserConfig", "Password"] = "";
                WenConfig.Save();
                LoginForm loginForm = new LoginForm();
                if (loginForm.ShowDialog() != DialogResult.OK)
                {
                    Application.Exit();
                }
                else
                {
                    form.Invoke(new Action(() =>
                    {
                        form.Show();
                    }));
                }
            });
        }
    }
}
